<?php

namespace App\Models\Admin;

use App\Log\GoodsDatumAdminLog;
use Illuminate\Database\Eloquent\Model;
use DB,Saas;

class GoodsDatum extends Model
{
    const OPERATE_PID = 2;
    const MODEL = 'goods_datum';

    protected $table = 'goods_datum';

    public $timestamps = false;

    protected $guarded = [];

    public static function getDatum($type)
    {
        return self::where('type',$type)
            ->where('company_id',0)
            ->select('title','id','status')
            ->get()->toArray();
    }

    public static function updateDatum($data,$type){
        return DB::table('goods_datum_company')->where('type',$type)->where('company_id',Saas::id())->update($data);
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new GoodsDatumAdminLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
